package com.fairytask.ui.main
{
	import com.fairytask.data.RecordObject;
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.define.FunDefine;
	import com.fairytask.ui.ActiveDegreeUI;
	import com.fairytask.ui.AlchemyUT;
	import com.fairytask.ui.ArenaUI;
	import com.fairytask.ui.ContinuesOnlineGiftUI;
	import com.fairytask.ui.FirstPayAwardUI;
	import com.fairytask.ui.GetEquipUI;
	import com.fairytask.ui.GetPetsUI;
	import com.fairytask.ui.MainUI;
	import com.fairytask.ui.MillionStupaUI;
	import com.fairytask.ui.TopUpRewardUI;
	import com.fairytask.ui.VipUI;
	import com.fairytask.ui.WantedListUI;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.effect.TopBtnEffect;
	import com.greensock.TimelineMax;
	import com.greensock.TweenAlign;
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	
	/**
	 *  顶部功能图标
	 * 
	 */
	public class TopUI extends FrameUI
	{
		public static var _self:TopUI;
		
		public var onlineBtn		:Button;	//在线礼
		public var strongBtn		:Button;	//乾坤炉
		public var alchemyBtn		:Button;	//炼金炉
		public var towerBtn		:Button;	//万灵塔
		public var fightBtn		:Button;	//擂台
		public var postBtn			:Button;	//官职
		public var firstPayBtn		:Button;	//首充礼
		public var dailyBtn		:Button;	//日常
		public var tomorrowBtn		:Button;	//明日礼
		public var activeBtn		:Button;	//活跃度
		public var yellowBtn		:Button;	//仙灵鉴
		public var favoriteBtn		:Button;	//收藏
		public var wantedTaskBtn   :Button;    //通缉榜
		public var getEquipBtn		:Button;	//神兵谱
		
		public var vipBtn			:Button;	//购买vip
		public var vipDayAwardBtn	:Button;	//vip每日领奖
		public var topUpRewardBtn	:Button;	//充值大回馈
		
		
		public var timeLineVip:TimelineMax;
		
		//功能开启(等级，按钮)
		private var _openArr	:Array = [];
		//当前开启按钮数量
		private var _curBtnNum	:int;
		//单行按钮6个
		private const LINE_NUM	:int = 6;
		//开始位置
		private const START_X	:int = 365;
		//在播放动画
		private var _isMoving	:Boolean = false;
		private var _openCount	:int;
		//是否收藏
		private var _hasCollect	:Boolean = false;
		//收藏动画时间轴
		private var timeLine     :TimelineMax;
		private var timeLineAct  :TimelineMax;	//活跃度
		private var timeLineTop  :TimelineMax;
		private var timeLineFirst:TimelineMax;//首充好礼
		
		
		private var _showBtnArray:Array = [];		//当有按钮显示的时候就放入数组，用于当刷新的时候进行刷新
		private var _unbind		:Array	=	[];		//存放进行解绑数据的数组
		private var _onlineUI		:ContinuesOnlineGiftUI = ContinuesOnlineGiftUI.getInstance();
		
		public static function getInstance():TopUI
		{
			if(_self==null)
			{
				_self=new TopUI();
			}
			return _self;
		}
		
		public function TopUI(popUI:Boolean=false)
		{
			super(popUI);
			initArr();
			initEvent();
			initTimeLine();
		}
		
		/**
		 * 初始化动画 
		 * 
		 */
		private function initTimeLine():void
		{
			//收藏动画效果初始化
			timeLine = new TimelineMax({repeat:-1});
			timeLine.appendMultiple([TweenMax.to(favoriteBtn, 0.2, {glowFilter:{color:0xFF0000, alpha:1, blurX:30, blurY:30,strength:1.5}, ease:Linear.easeNone}),
				TweenMax.to(favoriteBtn, 0.1, {glowFilter:{color:0xFF0000, alpha:1, blurX:0, blurY:0}, ease:Linear.easeNone})],0,TweenAlign.SEQUENCE);
			timeLine.stop();
			
			//活跃度动画效果初始化
			timeLineAct = new TimelineMax({repeat:-1});
			timeLineAct.appendMultiple([TweenMax.to(activeBtn, 0.2, {glowFilter:{color:0xFF0000, alpha:1, blurX:30, blurY:30,strength:1.5}, ease:Linear.easeNone}),
				TweenMax.to(activeBtn, 0.1, {glowFilter:{color:0xFF0000, alpha:1, blurX:0, blurY:0}, ease:Linear.easeNone})],0,TweenAlign.SEQUENCE);
			timeLineAct.stop();
			ActiveDegreeUI.getInstance();
			
			//充值回馈动画效果初始化
			timeLineTop = new TimelineMax({repeat:-1});
			timeLineTop.appendMultiple([TweenMax.to(topUpRewardBtn, 0.2, {glowFilter:{color:0xFF0000, alpha:1, blurX:30, blurY:30,strength:1.5}, ease:Linear.easeNone}),
				TweenMax.to(topUpRewardBtn, 0.1, {glowFilter:{color:0xFF0000, alpha:1, blurX:0, blurY:0}, ease:Linear.easeNone})],0,TweenAlign.SEQUENCE);
			timeLineTop.stop();
			
			//vip动画效果初始化
			timeLineVip = new TimelineMax({repeat:-1});
			timeLineVip.appendMultiple([TweenMax.to(vipBtn, 0.2, {glowFilter:{color:0xFF0000, alpha:1, blurX:30, blurY:30,strength:1.5}, ease:Linear.easeNone}),
				TweenMax.to(vipBtn, 0.1, {glowFilter:{color:0xFF0000, alpha:1, blurX:0, blurY:0}, ease:Linear.easeNone})],0,TweenAlign.SEQUENCE);
			timeLineVip.stop();
			
			//首充好礼效果初始化
			timeLineFirst = new TimelineMax({repeat:-1});
			timeLineFirst.appendMultiple([TweenMax.to(firstPayBtn, 0.2, {glowFilter:{color:0xFF0000, alpha:1, blurX:30, blurY:30,strength:1.5}, ease:Linear.easeNone}),
				TweenMax.to(vipBtn, 0.1, {glowFilter:{color:0xFF0000, alpha:1, blurX:0, blurY:0}, ease:Linear.easeNone})],0,TweenAlign.SEQUENCE);
			timeLineFirst.stop();
		}
		
		/**
		 * 初始化数组 
		 * 
		 */
		private function initArr():void
		{
			
			_openArr[FunDefine.vip]			= "vipBtn";
			_openArr[FunDefine.PETHURT]		= "yellowBtn";
			_openArr[FunDefine.WORKSHOP]	= "strongBtn";
			_openArr[FunDefine.ARENA]		= "fightBtn";
			_openArr[FunDefine.ALCHEMY]		= "alchemyBtn";
			_openArr[FunDefine.WANLINTOWER]	= "towerBtn";
			_openArr[FunDefine.COLLECT]	= "favoriteBtn";
			_openArr[FunDefine.WANTEDTASK] = "wantedTaskBtn";
			_openArr[FunDefine.FIRSTPAY]   = "firstPayBtn";
			_openArr[FunDefine.vipDayAward] = "vipDayAwardBtn";
			_openArr[FunDefine.TopUpReward] = "topUpRewardBtn";
			_openArr[FunDefine.GETEQUIP] = "getEquipBtn";
			_openArr[FunDefine.ACTIVEDEGREE] = "activeBtn";
//			initBtn(FunDefine.vip);
//			initBtn(FunDefine.TopUpReward);
			//initBtn(FunDefine.FIRSTPAY);
			//initBtn(FunDefine.vipDayAward);
		}
		
		/**
		 * 初始化事件 
		 * 
		 */
		private function initEvent():void
		{
			fightBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			strongBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			alchemyBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			yellowBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			towerBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			wantedTaskBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			favoriteBtn.addEventListener(MouseEvent.CLICK,onFavorite);
			vipBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			firstPayBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			vipDayAwardBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			topUpRewardBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			getEquipBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			activeBtn.addEventListener(MouseEvent.CLICK,onshowUI);
			//主界面开启功能列表
			//			model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_FUNC_LIST, initBtn);
			//主界面开启功能列表
			//			model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_FUNC_OPEN, openBtn);
			//监听玩家准备完毕
			//model.addEventListener(LogicEvent.USERLOADED, onUserReady);
			//model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_LAST_ONLINE_PRIZE,hideOnlineUI);
			//model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_ONLOAD_SHOW,showOnlineUI);
			
			//活跃度按钮发光
			//model.addEventListener(ActiveBtnEvent.BTN_FLASH,onActFlash);
			//model.addEventListener(ActiveBtnEvent.BTN_FLASH_STOP,onActFlashStop);
		}
		
		/**
		 * 活跃度按钮发光 
		 * @param evt
		 * 
		 */
		/*private function onActFlash(evt:ActiveBtnEvent):void
		{
			timeLineAct.play();
		}*/
		
		/**
		 * 活跃度按钮取消发光 
		 * @param evt
		 * 
		 */
		/*private function onActFlashStop(evt:ActiveBtnEvent):void
		{
			timeLineAct.stop();
			activeBtn.filters	=	null;
		}*/
		
		/**
		 *服务器给消息是否显示onLineUI 
		 * @param evt
		 * 
		 */		
		/*private function showOnlineUI(evt:CustomToClientEvent):void
		{
			if(!windowBG.contains(_onlineUI))
			{
				windowBG.addChild(_onlineUI);
				_onlineUI.x = 	START_X - (_curBtnNum)%LINE_NUM*75;
				_onlineUI.y = 	Math.floor((_curBtnNum)/LINE_NUM)*80;
			}
		}*/
		
		/**
		 *收到消息移除在线奖励的按钮
		 * @param evt
		 * 
		 */		
		/*private function hideOnlineUI(evt:CustomToClientEvent):void
		{
			if(windowBG.contains(_onlineUI))
			{
				windowBG.removeChild(_onlineUI);
			}
		}*/
		
		/**
		 * 绑定玩家数据 
		 */
		/*private function onUserReady(evt:LogicEvent):void
		{
			bindManager.BindProp(Player.getInstance().data,"Collect",this,"pCollect");
			
			//绑定vip的表
			bindManager.BindRecord(model.scene,RecordDefine.VIPPRILEGEREC,onAddVip,onRemoveVip);
			
			//绑定是否需要对充值回馈的按钮特效处理
			bindManager.BindProp(Player.getInstance().data,"PrizeCount",this,"PPrizeCount");
			bindManager.BindProp(Player.getInstance().data,"AwardGet",this,"firetPrize");
			VipUI.getInstance();
			
		}*/
		
		/**
		 *对其进行绑定 
		 * @param val
		 * 
		 */		
		public function set PPrizeCount(val:int):void
		{
			if(val==0)
			{
				topUpRewardBtn.filters = null;
				timeLineTop.stop();
			}
			else
			{
				
				timeLineTop.play();
			}
		}
		/**
		 * 玩家是否已经领取首充好礼
		 * 0:不可领  1：可以领  2 已领取 
		 * @param val
		 */
		public function set firetPrize(val:int):void
		{
			switch(val)
			{
				case 1:
					timeLineFirst.play();
				case 0:
					initBtn(FunDefine.FIRSTPAY);
					break;
				case 2:
					refresh("firstPayBtn");
					timeLineFirst.stop();
					firstPayBtn.filters = null;
					break;
			}
		}
		
		/**
		 *添加表行的记录 
		 * @param key
		 * @param val
		 * 
		 */		
		private function onAddVip(key:String,val:RecordObject):void
		{
			var type:int = val.GetProperties("0");
			//类型为1就代表是日奖励的类型
			if(type==2)
			{
				_unbind.push([val,"2",bindManager.BindRecordProp(val,"2",changStatue,[key])]);
			}	
		}
		
		/**
		 *修改领取的状态 
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */		
		private function changStatue(key:String,val:int,pList:Array):void
		{
			//当领取了奖励后就进行修改重新显示界面
			if(val==1)
			{
				if(windowBG.contains(vipDayAwardBtn))
				{
					refresh("vipDayAwardBtn");
				}
			}
			if(val==0)
			{
				if(!windowBG.contains(vipDayAwardBtn))
				{
					initBtn(FunDefine.vipDayAward);
				}
			}
			
		}
		
		/**
		 *删除表行的记录 
		 * @param key
		 * @param val
		 * 
		 */		
		private function onRemoveVip(key:String,val:RecordObject):void
		{
			var type:int = val.GetProperties("0");
			if(type==2)
			{
				for each(var itemBind:Array in _unbind)
				{
					bindManager.UnbindProp(itemBind.shift(),itemBind.shift(),itemBind.shift());
				}	
				refresh("vipDayAwardBtn");
			}
		}
		
		/**
		 * 点击收藏 
		 * @param evt
		 * 
		 */
		private function onFavorite(evt:MouseEvent):void
		{
			if(!GameStage.isLocal)
			{
				ExternalInterface.call("addFavoriteFormAS");
			}
			if(!_hasCollect)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ENSHRINE);
			}
		}
		
		/**
		 * 绑定是否收藏 
		 * @param val
		 * 
		 */
		public function set pCollect(val:int):void
		{
			_hasCollect = Boolean(val);
			checkCollect();
		}
		
		/**
		 * 检查收藏是否发光
		 * 
		 */
		private function checkCollect():void
		{
			//收藏了
			if(_hasCollect)
			{
				favoriteBtn.filters = null;
				timeLine.stop();
			}
				//没收藏
			else
			{
				timeLine.play();
			}
		}
		
		/**
		 * 点击按钮显示ui 
		 * @param evt
		 * 
		 */
		private function onshowUI(evt:MouseEvent):void
		{
			ArenaUI.getInstance().changeHide();
			switch(evt.currentTarget)
			{
				case strongBtn:
					isShowUI(MainUI.getInstance(),2);
					break;
				case alchemyBtn:
					isShowUI(AlchemyUT.getInstance());
					break;
				case fightBtn:
					isShowUI(ArenaUI.getInstance());
					break;
				case yellowBtn:
					if(!GetPetsUI.getInstance().isOpen())
					{
						//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_HUNTER_PET);
					}
					else
					{
						GetPetsUI.getInstance().hide();
					}
					break;
				case towerBtn:
					isShowUI(MillionStupaUI.getInstance());
					break;
				case wantedTaskBtn:
					isShowUI(WantedListUI.getInstance());
					break;
				case vipBtn:
					VipUI.getInstance().show();
					break;
				case vipDayAwardBtn:
					sendGetAwardMessage();
					break;
				case firstPayBtn:
					isShowUI(FirstPayAwardUI.getInstance());
					break;
				case topUpRewardBtn:
					TopUpRewardUI.getInstance().show();
					break;
				case getEquipBtn:
					isShowUI(GetEquipUI.getInstance());
					break;
				case activeBtn:
					isShowUI(ActiveDegreeUI.getInstance());
					break;
			}
		}
		
		/**
		 *当点击的是领取奖励的按钮的时候
		 * 1 给服务端发送领取奖励的消息 
		 * 
		 */		
		private function sendGetAwardMessage():void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_VIP_PRIZE,1);
		}
		
		/**
		 *刷新显示 
		 *  1 删除领奖的按钮
		 * 2重新刷新topUI说有按钮的显示位置
		 * 
		 */		
		private function refresh(val:String):void
		{
			if(!windowBG.contains(this[val]))
			{
				return;
			}
			var str:String = val;
			for(var j:int=0;j<_showBtnArray.length;j++)
			{
				if(_showBtnArray[j]==str)
				{
					windowBG.removeChild(this[_showBtnArray[j]]);
					_showBtnArray.splice(j,1);
					_curBtnNum--;
					break;
				}
			}
			for(var i:int=0;i<_showBtnArray.length;i++)
			{
				this[_showBtnArray[i]].x = START_X - i%LINE_NUM*75;
				this[_showBtnArray[i]].y = Math.floor(i/LINE_NUM)*80;
			}
			_onlineUI.x = 	START_X - (_curBtnNum)%LINE_NUM*75;
			_onlineUI.y = 	Math.floor((_curBtnNum)/LINE_NUM)*80;
			
		}
		
		/**
		 * 
		 * 首先放放好在线好礼的位置
		 */		
		private function downLocation():void
		{
			
			_onlineUI.x = 	START_X - (_curBtnNum+1)%LINE_NUM*75;
			_onlineUI.y = 	Math.floor((_curBtnNum+1)/LINE_NUM)*80;
		}
		
		/**
		 * 初始化按钮 
		 * 
		 */
		public function initBtn(id:int):void
		{
			//如果包含该按钮就返回
			if(windowBG.contains(this[_openArr[id]]))
			{
				return;
			}
			//首先放放好在线好礼的位置
			downLocation();
			
			this[_openArr[id]].x = START_X - _curBtnNum%LINE_NUM*75;
			this[_openArr[id]].y = Math.floor(_curBtnNum/LINE_NUM)*80;
			windowBG.addChild(this[_openArr[id]]);
			_curBtnNum++;
			_showBtnArray.push(_openArr[id]);
		}
		
		/**
		 * 开启按钮特效，播放动画
		 * 
		 */
		public function openBtn(id:int):void
		{
			//首先放放好在线好礼的位置
			downLocation();
			var mc	:Button = this[_openArr[id]] as Button;
			var _x :int = START_X - _curBtnNum%LINE_NUM*75 + this.x;
			var _y :int = Math.floor(_curBtnNum/LINE_NUM)*80 + this.y;
			var effect	:TopBtnEffect = new TopBtnEffect(mc,_x,_y,onComplete,[START_X - _curBtnNum%LINE_NUM*75,Math.floor(_curBtnNum/LINE_NUM)*80]);
			effect.show();
			_isMoving = true;
			_curBtnNum++;
			_showBtnArray.push(_openArr[id]);
			_openCount++;
		}
		
		/**
		 * 完成播放,添加到位置
		 * @param mc
		 * 
		 */
		private function onComplete(mc:DisplayObject,arr:Array):void
		{
			mc.x = arr[0];
			mc.y = arr[1];
			windowBG.addChild(mc);
			//添加的是收藏的时候
			if(mc == favoriteBtn)
			{
				checkCollect();
			}
			_openCount--;
			//去除遮罩
			if(_openCount == 0)
			{
				//FunctionLogic.instance.hasMask = false;
				_isMoving = false;
				//model.dispatchEvent(new GuideEvent(GuideEvent.MOVING_COMPLETE));
			}
		}
		
		/**
		 * 是否打开，打开则关闭，未打开打开 
		 * @param ui
		 * @param pos
		 * 
		 */
		private function isShowUI(ui:FrameUI,pos:int=-1):void
		{
			if(!ui.isOpen(pos))
			{
				ui.showPos(pos);
			}
			else
			{
				ui.hide();
			}
		}
		
		public function get isMoving():Boolean
		{
			return _isMoving;
		}
		
		override public function show(space:String=BACKLAYER, onshow:Function=null):void
		{
			super.show(space,onshow);
		}
		
		/**
		 * 场景变化改变位置 
		 * 
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			this.x	=	initX + GameStage.mainLayer.x;
			this.y = 	initY - GameStage.mainLayer.y;
		}
	}
}